package com.zxjbyte.yiyi.module.system.service.dict.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.exception.ServiceException;
import com.zxjbyte.yiyi.framework.common.util.AuthUtil;
import com.zxjbyte.yiyi.framework.common.util.CollXUtil;
import com.zxjbyte.yiyi.framework.mybatis.service.impl.BaseServiceXImpl;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.system.domain.entity.DictTypePO;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictTypeAddRequest;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictTypePageRequest;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypeDetailResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypePageResponse;
import com.zxjbyte.yiyi.module.system.mapper.dict.DictTypeMapper;
import com.zxjbyte.yiyi.module.system.service.dict.IDictTypeService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.zxjbyte.yiyi.framework.common.enums.SortOrder.DESC;

/**
 * @Author zhangxingjia
 * @Date 2024/1/24 16:18
 * @Version: 1.0
 */
@Service
public class DictTypeServiceImpl extends BaseServiceXImpl<DictTypeMapper, DictTypePO> implements IDictTypeService {

    @Override
    public PageResult<DictTypePageResponse> page(DictTypePageRequest request) {
        LambdaQueryWrapper<DictTypePO> wrapper = Wrappers.<DictTypePO>lambdaQuery().ge(Objects.nonNull(request.getType()), DictTypePO::getType, request.getType())
                .eq(Objects.nonNull(request.getStatus()), DictTypePO::getStatus, request.getStatus())
                .like(StrUtil.isNotBlank(request.getName()), DictTypePO::getName, request.getName());
        IPage<DictTypePO> page = page(Condition.getPage(request, DictTypePO::getCreateTime, DESC), wrapper);
        List<DictTypePO> records = page.getRecords();
        List<DictTypePageResponse> responseList = null;
        if(CollXUtil.isNotEmpty(records)){
            responseList = CglibUtil.copyList(records, DictTypePageResponse::new);
        }
        return PageResult.of(responseList, page);
    }

    @Override
    public DictTypeDetailResponse detail(Long id) {
        DictTypePO entity = getById(id);
        if(Objects.isNull(entity)){
            throw new ServiceException("字典类型不存在");
        }
        return CglibUtil.copy(entity, DictTypeDetailResponse.class);
    }

    @Override
    public void add(DictTypeAddRequest request) {
        checkDictTypeRepeat(request.getType());
        DictTypePO entity = new DictTypePO();
        CglibUtil.copy(request, entity);
        Date now = new Date();
        String username = AuthUtil.getUsername();
        entity.setCreateTime(now);
        entity.setCreateUser(username);
        entity.setUpdateTime(now);
        entity.setUpdateUser(username);
        save(entity);
    }

    @Override
    public List<DictTypePO> getByName(String name) {
        return lambdaQuery().like(StrUtil.isNotBlank(name), DictTypePO::getName, name).list();
    }

    private void checkDictTypeRepeat(String dictType){
        if(count(Wrappers.<DictTypePO>lambdaQuery().eq(DictTypePO::getType, dictType)) > 0){
            throw new ServiceException("字典类型已存在");
        }
    }
}
